package cn.plushine.controller;


import cn.plushine.pojo.Supplier;
import cn.plushine.service.BaseSupplierService;
import cn.plushine.service.BaseSupplierServiceImpl;
import cn.plushine.utils.ResMapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Api(tags = "供应商信息")
@RestController
@RequestMapping("/admin/product/supplier")
public class BaseSupplierController {

    @Autowired
    BaseSupplierService baseSupplierService;

    // 获取供应商信息
    @ApiOperation(value = "获取供应商信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "input", value = "关键字", paramType = "query", dataType = "String")
    })
    @GetMapping("/get")
    public HashMap<String, Object> delete(String input) {
        try {
            // 获取供应商信息
            List<Supplier> supplierListDB = baseSupplierService.get(input);
            // 成功响应
            return ResMapUtils.encapsulation(200, "获取供应商信息成功！", supplierListDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "获取供应商信息失败！", null);
        }
    }

    // 添加供应商信息
    @ApiOperation(value = "添加供应商信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "supplier", value = "供应商信息", required = true, paramType = "body", dataType = "Supplier")
    })
    @PostMapping("/add")
    public HashMap<String, Object> add(@RequestBody Supplier supplier) {
        try {
            // 添加供应商信息
            int addDB = baseSupplierService.add(supplier);
            // 成功响应
            return ResMapUtils.encapsulation(200, "添加供应商信息成功！", addDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "添加供应商信息失败！", null);
        }
    }

    // 修改供应商信息
    @ApiOperation(value = "修改供应商信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "supplier", value = "供应商信息", required = true, paramType = "body", dataType = "Supplier")
    })
    @PutMapping("/modify")
    public HashMap<String, Object> modify(@RequestBody Supplier supplier) {
        try {
            // 修改供应商信息
            baseSupplierService.modify(supplier);
            // 成功响应
            return ResMapUtils.encapsulation(200, "修改供应商信息成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "修改供应商信息失败！", e.getMessage());
        }
    }

    // 删除供应商信息
    @ApiOperation(value = "删除供应商信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "供应商id", example = "1", required = true, paramType = "path", dataType = "int")
    })
    @DeleteMapping("/delete/{id}")
    public HashMap<String, Object> delete(@PathVariable int id) {
        try {
            // 修改供应商信息
            baseSupplierService.delete(id);
            // 成功响应
            return ResMapUtils.encapsulation(200, "删除供应商信息成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "删除供应商信息失败！", e.getMessage());
        }
    }
}
